package com.system.dialog.datatime;

import android.view.View;
import android.widget.TextView;


import com.system.dialog.R;

import java.util.Arrays;
import java.util.List;

public class WheelDateMain {

	private View view;
	private WheelView wv_year;
	private WheelView wv_month;
	private WheelView wv_day;
	private WheelView wv_hours;
	private WheelView wv_mins;
    private TextView mYearText;
    private TextView mMonthText;
    private TextView mDayText;
    private TextView mHourText;
    private TextView mMinText;


    public int screenheight;
	private boolean hasSelectTime;
	private static int START_YEAR = 1990, END_YEAR = 2100;

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public int getSTART_YEAR() {
		return START_YEAR;
	}

	public void setSTART_YEAR(int sTART_YEAR) {
		START_YEAR = sTART_YEAR;
	}

	public int getEND_YEAR() {
		return END_YEAR;
	}

	public void setEND_YEAR(int eND_YEAR) {
		END_YEAR = eND_YEAR;
	}

	public WheelDateMain(View view) {
		super();
		this.view = view;
		hasSelectTime = false;
		setView(view);
	}

	public WheelDateMain(View view, boolean hasSelectTime) {
		super();
		this.view = view;
		this.hasSelectTime = hasSelectTime;
		setView(view);
	}

	public void initDateTimePicker(int year, int month, int day) {
		this.initDateTimePicker(year, month, day, 0, 0);
	}

	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	public void initDateTimePicker(int year, int month, int day, int h, int m) {
		// int year = calendar.get(Calendar.YEAR);
		// int month = calendar.get(Calendar.MONTH);
		// int day = calendar.get(Calendar.DATE);
		// 添加大小月月份并将其转换为list,方便之后的判断
		String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
		String[] months_little = { "4", "6", "9", "11" };

		final List<String> list_big = Arrays.asList(months_big);
		final List<String> list_little = Arrays.asList(months_little);

		// 年
		wv_year = (WheelView) view.findViewById(R.id.year);
        mYearText = (TextView) view.findViewById(R.id.year_text);
        wv_year.setAdapter(new NumericWheelAdapter(START_YEAR, END_YEAR));// 设置"年"的显示数据
		wv_year.setCyclic(true);// 可循环滚动
//		wv_year.setLabel("年");// 添加文字
		wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

		// 月
		wv_month = (WheelView) view.findViewById(R.id.month);
        mMonthText = (TextView) view.findViewById(R.id.month_text);
        wv_month.setAdapter(new NumericWheelAdapter(1, 12));
		wv_month.setCyclic(true);
//		wv_month.setLabel("月");
		wv_month.setCurrentItem(month);

		// 日
		wv_day = (WheelView) view.findViewById(R.id.day);
        mDayText = (TextView) view.findViewById(R.id.day_text);
        wv_day.setCyclic(true);
		// 判断大小月及是否闰年,用来确定"日"的数据
		if (list_big.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 31));
		} else if (list_little.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 30));
		} else {
			// 闰年
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				wv_day.setAdapter(new NumericWheelAdapter(1, 29));
			else
				wv_day.setAdapter(new NumericWheelAdapter(1, 28));
		}
//		wv_day.setLabel("日");
		wv_day.setCurrentItem(day - 1);

		wv_hours = (WheelView) view.findViewById(R.id.hour);
        mHourText = (TextView) view.findViewById(R.id.hour_text);
        wv_mins = (WheelView) view.findViewById(R.id.min);
        mMinText = (TextView) view.findViewById(R.id.min_text);
        if (hasSelectTime) {
			wv_hours.setVisibility(View.VISIBLE);
			wv_mins.setVisibility(View.VISIBLE);
            mHourText.setVisibility(View.VISIBLE);
            mMinText.setVisibility(View.VISIBLE);
			wv_hours.setAdapter(new NumericWheelAdapter(0, 23));
			wv_hours.setCyclic(true);// 可循环滚动
//			wv_hours.setLabel("时");// 添加文字
			wv_hours.setCurrentItem(h);

			wv_mins.setAdapter(new NumericWheelAdapter(0, 59));
			wv_mins.setCyclic(true);// 可循环滚动
//			wv_mins.setLabel("分");// 添加文字
			wv_mins.setCurrentItem(m);
		} else {
			wv_hours.setVisibility(View.GONE);
			wv_mins.setVisibility(View.GONE);
            mHourText.setVisibility(View.GONE);
            mMinText.setVisibility(View.GONE);
		}

		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big
						.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(wv_month
						.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);

		// 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
		if (m_textSize == 0) {
			if (hasSelectTime)
				m_textSize = (screenheight / 100) * 3;
			else
				m_textSize = (screenheight / 100) * 4;
		}
		wv_day.TEXT_SIZE = m_textSize;
		wv_month.TEXT_SIZE = m_textSize;
		wv_year.TEXT_SIZE = m_textSize;
		wv_hours.TEXT_SIZE = m_textSize;
		wv_mins.TEXT_SIZE = m_textSize;
		if (mWidth != 0 || mHeight != 0) {
			wv_day.setWidthHeight(mWidth, mHeight);
			wv_month.setWidthHeight(mWidth, mHeight);
			wv_year.setWidthHeight(mWidth, mHeight);
			wv_hours.setWidthHeight(mWidth, mHeight);
			wv_mins.setWidthHeight(mWidth, mHeight);
		}
	}

	private int m_textSize = 0;

	public void setTextSize(int textSize) {
		m_textSize = textSize;
	}

	private int mWidth, mHeight;

	public void setWidthHeight(int width, int height) {
		this.mWidth = width;
		this.mHeight = height;
	}

	public String getTime() {
		String date = String.format("%04d-%02d-%02d",
				(wv_year.getCurrentItem() + START_YEAR),
				(wv_month.getCurrentItem() + 1), (wv_day.getCurrentItem() + 1));
		if (hasSelectTime) {
			date += String.format(" %02d:%02d", wv_hours.getCurrentItem(),
					wv_mins.getCurrentItem());
		}
		return date;
	}
}
